package edu.ncsu.csc.ip.client.run;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.UnknownHostException;
import edu.ncsu.csc.ip.client.PeerClient;
import edu.ncsu.csc.ip.common.Log;

public class RunTask3 {

	public static void main(String[] args) {

		String[] addressStrings = {"152.1.13.82", "152.46.16.215", "152.1.13.83"};

		long startTime = 0;
		long endTime = 0;
		long duration = 0;
		long avg = 0;
		String filename = "test.txt";
		int MSS = 500;
		int port = 65424;
		
		InetAddress[] addressList = new InetAddress[addressStrings.length];
		
		for(int i = 0; i < (addressStrings.length); i++){
			try {
				addressList[i] = InetAddress.getByName(addressStrings[i]);
			} catch (UnknownHostException e) {
				Log.println("IP Address: " + addressStrings[i] + " is malformed.");
				System.exit(0);
			}
		}
		
		PeerClient pc = new PeerClient(addressList, port, filename, MSS);
		Log.println("Starting test");
		try {
//			PrintWriter printWrite = new PrintWriter(new FileWriter(file,
//					false));
//			printWrite.println("Test# , Time in ms");
			for (int i = 1; i < 5; i++) {
				startTime = System.currentTimeMillis();
				pc.runClient();
				endTime = System.currentTimeMillis();
				duration = endTime - startTime;
				avg += duration;
				File file = new File("./Task3_round"+i+"_results.csv");
				PrintWriter printWrite = new PrintWriter(new FileWriter(file,
						false));
				printWrite.println("Test# , Time in ms");
				printWrite.println(i + "," + duration);
				printWrite.close();
			}
//			avg = avg / 5;
//			printWrite.println("Average" + "," + avg);
//			printWrite.println("Average in seconds" + "," + avg / 1000);
//			printWrite.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		Log.println("End of test");
		avg = 0;
		System.exit(0);
	}
}
